package com.hzjy.manage.controller;


import com.hzjy.api.beans.HzjyProduct;
import com.hzjy.api.service.impl.ApiProductServiceImpl;
import com.hzjy.common.core.controller.BaseController;
import com.hzjy.common.core.domain.AjaxResult;
import com.hzjy.common.core.page.TableDataInfo;
import com.hzjy.common.enums.TradingStatusEnum;
import com.hzjy.common.utils.PageData;
import com.hzjy.manage.service.impl.HzjyProductServiceImpl;
import com.hzjy.manage.vo.HzjyProductDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 挂牌清单
 */

@Api("挂牌清单")
@RestController
@RequestMapping("/manage/audit_trade")
public class AuditTradeController extends BaseController {

    @Autowired
    private HzjyProductServiceImpl hzjyProductService;
    @Autowired
    private ApiProductServiceImpl apiProductService;


    @ApiOperation("获取挂牌清单集合")
    @PreAuthorize("@ss.hasPermi('manage:audit_trade:list')")
    @GetMapping("/list")
    public TableDataInfo auditTradeList(String productName,Integer tradingType) {
        startPage();
        List<HzjyProductDTO> hzjyProductList = hzjyProductService.selectProductList(productName,tradingType);
        return getDataTable(hzjyProductList);
    }

    @ApiOperation("上架")
    @PreAuthorize("@ss.hasPermi('manage:audit_trade:up')")
    @PostMapping("/upTrading")
    public AjaxResult upTrading(@RequestBody PageData pd) {

        Object productId = pd.get("product_id");
        if (productId != null) {
            HzjyProduct hzjyProduct = apiProductService.selectProductInfo(Long.valueOf(productId.toString()));
            if (hzjyProduct.getState() >= TradingStatusEnum.TradingSuccess.getCode()) {
                if (hzjyProduct.getUpState() == 1) {
                    return AjaxResult.error("上架失败,该作品已上架.");
                }
                apiProductService.upTrading(pd);
                return AjaxResult.success("上架成功");
            } else {
                return AjaxResult.error("上架失败,该作品未挂牌成功.");
            }
        } else {
            return AjaxResult.error(500, "参数为空");
        }
    }

    @ApiOperation("下架")
    @PreAuthorize("@ss.hasPermi('manage:audit_trade:low')")
    @PostMapping("/lowTrading")
    public AjaxResult lowTrading(@RequestBody PageData pd) {

        Object productId = pd.get("product_id");
        if (productId != null) {
            HzjyProduct hzjyProduct = apiProductService.selectProductInfo(Long.valueOf(productId.toString()));
            if (hzjyProduct.getUpState() != null && hzjyProduct.getUpState() == 1) {
                apiProductService.lowTrading(pd);
                return AjaxResult.success("下架成功");
            } else {
                return AjaxResult.error("下架失败,该作品未上架.");
            }
        } else {
            return AjaxResult.error(500, "参数为空");
        }
    }
}
